package com.wpw.java8demo;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Random;
import java.util.UUID;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Predicate;

import org.junit.Before;
import org.junit.Test;

public class LambdaTest {
	
	List<Apple> appList;
	
	@Before
	public void before() {
		appList = new ArrayList<>();
		Apple app;
		for (int i = 0; i < 20; i++) {
			app = new Apple();
			app.setColor(String.valueOf(i));
			app.setWeigth(new Random().nextInt(10));
			appList.add(app);
		}
	}
	
	@Test
	public void test1() {
		Thread thread = new Thread(() -> System.out.println("hello world"));
		thread.start();
	}

	@Test
	public void test2() {
		//捕获lambda
		int i = 1;
		Runnable r = () -> {
			System.out.println("dddddddddddd");
			// i++; //编译错误
			System.out.println(i);
		};

		new Thread(r).start();

		System.out.println(i);
		//i = 2;//编译错误

	}
	
	@Test
	public void test3() {
		List<Integer> list = new ArrayList<>();
		for (int i = 0; i < 10; i++) {
			list.add(new Random().nextInt(100));
		}
		//list.sort((a,b)->a.compareTo(b));
		
		//list.sort((a, b) -> Integer.compare(a, b));
		
		//list.sort(Integer::compare);
		
		System.out.println(list);
	}
	
	@Test
	public void test4() {
		List<String> list = new ArrayList<>();
		for (int i = 0; i < 10; i++) {
			list.add(UUID.randomUUID().toString().substring(0, new Random().nextInt(10)));
		}
		
//		list.sort(Comparator.comparing(String::length));
		list.sort(Comparator.comparing(String::length).reversed());
		list.stream().forEach(System.out::println);
	}
	
	@Test
	public void test5() {
		/*Function<Integer,Apple> appleSuplier = Apple::new;
		Apple apple = appleSuplier.apply(10);*/
		
		BiFunction<Integer, String, Apple> appBf = Apple::new;
		
		Apple apple = appBf.apply(31, "red");

		
		System.out.println(apple);
	}
	
	private void play() {
		appList.stream().forEach(System.out::println);
	}
	
	@Test
	public void test6() {
		appList.sort(Comparator.comparing(Apple::getWeigth).thenComparing(Apple::getColor));
		play();
	}
	
	@Test
	public void test7() {
		Apple app = new Apple();
		app.setColor("1");
		app.setWeigth(1);
		
		Predicate<Apple> preApp = (a) -> a.getColor().equals("1");
		
		test7Fun(preApp, app);
		test7Fun(preApp.negate(), app);
		test7Fun(preApp.and(a -> a.getWeigth() == 1), app);
	}
	
	private void test7Fun(Predicate<Apple> preApp, Apple app) {
		System.out.println(preApp.test(app));
		Predicate<Apple> negate = preApp.negate();
	}

	@Test
	public void test8() {
		Function<String, String> f = (String s) -> s + " header";
		Function<String, String> g = (String s) -> s + " footer";
		String andThen = f.andThen(g).apply("吴鹏伟");
		String compose = f.compose(g).apply("吴鹏伟");
		System.out.println(andThen);
		System.out.println(compose);

	}

	@Test
	public void test9() {
		appList.forEach(System.out::println);
		System.out.println("-----------");
		appList.stream().forEach(System.out::println);
	}
}
